package com.example.smartcity_poverty.Base;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import java.util.List;

/**
 * ANDROID STUDIO -VERSION 4.0
 * email: 2506789532@qq.com
 * CodeAuthor： Ikun
 * <p>
 * explain: Java版的封装RecyclerView
 * 封装RecyclerView的好处是， 比赛的时候开发编写适配器的时候快一点。
 *
 *
 *
 **/
public abstract class JavaEditionRecyclerAdapter<E, VB extends ViewBinding>
        extends RecyclerView.Adapter<JavaEditionRecyclerAdapter<E, VB>.RvHolderAdapter> {
    // 数据源
    List<E> lists;
    // 布局ID
    Integer layoutID;

    public JavaEditionRecyclerAdapter(List<E> lists, Integer layoutID) {
        this.lists = lists;
        this.layoutID = layoutID;
    }

    // 填充数据
    abstract public void setData(VB mBinding, E data, int position, RvHolderAdapter holder);

    // 设置布局
    abstract public VB setInflater(LayoutInflater layoutInflater, ViewGroup parent);

    @Override
    public void onBindViewHolder(@NonNull RvHolderAdapter holder, int position) {

    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    @NonNull
    @Override
    public RvHolderAdapter onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RvHolderAdapter((View) setInflater(LayoutInflater.from(parent.getContext()), parent));
    }

    class RvHolderAdapter extends RecyclerView.ViewHolder {
        VB mBinding;

        public RvHolderAdapter(@NonNull View itemView) {
            super(itemView);
            itemView = this.mBinding.getRoot();
        }

        public void bindData(E data, int position) {
            setData(this.mBinding, data, position, this);
        }
    }
}
